package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	
    public class Bootstrap extends Sprite 
    {    
    	private var m_xml:XML;
    	private var m_site:Keelworks;
    	
        public function Bootstrap() 
        {	
        	// set paths from flash vars
        	var xmlURL:String = this.loaderInfo.parameters.xml;
        	var siteURL:String = this.loaderInfo.parameters.site;

			// load xml
            if(xmlURL) loadXML(xmlURL);
            else throw new Error("XML path undefined"); 
            	
            // load site
           	if(siteURL) loadSite(siteURL);
           	else throw new Error("Site path undefined"); 
        }
        
        private function loadXML(url:String):void
        {
        	var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler, false, 0, true);
        	urlLoader.addEventListener(Event.COMPLETE, this.xmlLoaded, false, 0, true);
        	urlLoader.load(new URLRequest(url));
        }
        
        private function xmlLoaded(event:Event):void
        {
        	event.target.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
        	event.target.removeEventListener(Event.COMPLETE, this.xmlLoaded);
        	
        	var xml:XML = new XML(event.target.data);
			xml.ignoreWhitespace = true;
			
			trace("-- XML loaded --");
			trace("xml test:"+xml.test);
        }
        
        private function loadSite(url:String):void
        {
        	var loader:Loader = new Loader();
        	loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler, false, 0, true);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.siteLoadingProgress, false, 0, true);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.siteLoaded, false, 0, true);
			loader.load(new URLRequest(url), new LoaderContext(false, ApplicationDomain.currentDomain, Security.sandboxType == Security.REMOTE ? SecurityDomain.currentDomain : null));
        }
        
        private function siteLoadingProgress(event:ProgressEvent):void
        {
        	var percent:Number = (event.bytesLoaded / event.bytesTotal) * 100;
			trace(int(percent) + "% loaded");
			
			//update loading progress
        }
        
        private function siteLoaded(event:Event):void
        {
        	trace("-- Site loaded --");
        	event.target.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
        	event.target.removeEventListener(ProgressEvent.PROGRESS, this.siteLoadingProgress);
			event.target.removeEventListener(Event.COMPLETE, this.siteLoaded);
			
			//add site swf to display list
			m_site = Keelworks(event.target.content);
			m_site.setData();
			addChild(m_site);
        }
 
        private function ioErrorHandler(event:IOErrorEvent):void
        {
        	throw new Error("IO ERROR loading resources. Check xml and swf paths");
        }
    }
}
